package com.yupi.yupicturebackend.manager.websocket;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

import javax.annotation.Resource;

@Slf4j
@Component
public class WebSocketConfig implements WebSocketConfigurer {
    @Resource
    private PictureEditHandler pictureEditHandler;
    @Resource
    private WsHandshakeInterceptor wsHandshakeInterceptor;
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        // 注册 WebSocket 处理器 PictureEditHandler，用于处理图片编辑相关的 WebSocket 请求
        // 设置处理器的访问路径为 "/ws/picture/edit"  类似于controller
        registry.addHandler(pictureEditHandler, "/ws/picture/edit")
                // 添加握手拦截器 WsHandshakeInterceptor，用于在握手阶段进行权限校验和参数设置
                .addInterceptors(wsHandshakeInterceptor)
                // 允许所有来源的跨域请求（CORS），适用于开发环境；生产环境建议配置具体域名
                .setAllowedOrigins("*");
    }
}
